import React, { useCallback, useRef } from 'react';
import { GModalButton, GTable } from 'geminis-react';
import { Terminate } from '@/pages/workflow/instance/terminate';

export default () => {
  const table = useRef<any>();

  const changeDate = useCallback((value: any) => {
    return { content: value, totalElements: value.lenth };
  }, []);

  const reloadTable = useCallback(() => {
    table.current.loadData();
  }, []);

  const tableSchema = {
    headerTitle: '流程实例',
    data: {
      url: '/api/workflow/history/process',
    },
    action: {
      row: {
        render: (record: any) => [
          !record.endTime &&
          <Terminate
            key={'terminate-' + record.id}
            processId={record.id}
            onSuccess={reloadTable}
          />,
          <GModalButton
            key={'task-' + record.id}
            text="活动实例"
            button={{
              type: 'link',
            }}
            modal={{
              width: '90wh',
            }}
          >
            <GTable
              schema={{
                data: {
                  url: '/api/workflow/history/process/' + record.id + '/task',
                },
              }}
              schemaPath="/workflow-pc/schema/instance/task-table.json"
              onDataLoaded={changeDate}
            />
          </GModalButton>,
        ],
      },
    },
  };

  return (
    <GTable
      ref={table}
      schema={tableSchema}
      schemaPath="/workflow-pc/schema/instance/table.json"
    />
  );
};
